package com.dingqb.myapplication.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

import com.dingqb.myapplication.R;

@SuppressLint("AppCompatCustomView")
public class LoadingView extends ImageView {
    int mLeft;
    int mTop;
    int mRight;
    int mBorttom;
    int currentCount = 0;
    public static final int IMG_NUM = 4;

    public LoadingView(Context context) {
        super(context);
        init();
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.e("dingg", "onLayout:left= " + left + ",top=" + top + ",right=" + right + ",bottom=" + bottom);

        mLeft = left;
        mTop = top;
        mRight = right;
        mBorttom = bottom;

    }

    @SuppressLint("WrongConstant")
    void init() {
        Log.e("dingqq", "init: ");
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100, 0);
//        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new AccelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                setTop(mTop - value);
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                setImageDrawable(getResources().getDrawable(R.drawable.shape_oval_red));
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                currentCount++;
                switch (currentCount % IMG_NUM) {
                    case 0:
                        setImageDrawable(getResources().getDrawable(R.drawable.shape_oval_red));
                        break;
                    case 1:
                        setImageDrawable(getResources().getDrawable(R.drawable.shape_oval_blue));
                        break;
                    case 2:
                        setImageDrawable(getResources().getDrawable(R.drawable.shape_oval_green));
                        break;
                    case 3:
                        setImageDrawable(getResources().getDrawable(R.drawable.shape_oval));
                        break;
                }

            }
        });
        valueAnimator.start();

    }
}
